package com.liu.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.liu.common.util.DateUtil;
import com.liu.common.util.EncryptUtil;
import com.liu.common.util.PageInfoUtil;
import com.liu.common.util.RegexUtil;

public class BaseController
{
	@Autowired
	protected DateUtil dateUtil;
	@Autowired
	protected EncryptUtil encryptUtil ;
	@Autowired
	protected RegexUtil regexUtil ; 
	
	/**
	 * 提示信息
	 */
	protected String info = "" ; 
	
	/**
	 * 搜索条件
	 * @param request 接收用户传过来的信息
	 * @return 返回在Map中添加好的搜索条件
	 */
	protected Map<String,Object>proccedSearch(HttpServletRequest request)
	{
		/*搜索条件*/
		Map<String,Object>condMap =  new HashMap<>(1);
		/*=====关键字开始======*/
		String keyword  = request.getParameter("keyword");
		if(keyword == null)
		{
			keyword = "" ;
		}
		//去除前后空格
		keyword = keyword.trim();
		condMap.put("keyword", keyword);
		request.setAttribute("keyword", keyword);
		/*=====关键字结束======*/
		
		/*=====状态开始======*/
		String status  = request.getParameter("status");
		if(status == null)
		{
			status = "" ;
		}
		condMap.put("status", status);
		request.setAttribute("status", status);
		/*=====状态结束======*/
		
		/*=====时间开始======*/
		Date stDate = null ;
		Date edDate = null ;
		String st  = request.getParameter("st");
		if(st == null)
		{
			st = "" ;
		}
		String ed  = request.getParameter("ed");
		if(ed == null)
		{
			ed = "" ;
		}
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.strToDateTime(st) ; 
			edDate = this.dateUtil.strToDateTime(ed) ; 
		}
		/* 搜索条件 */
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		/*=====时间结束======*/
		
		return condMap ;
	}
	/**
	 * 分页工具信息
	 * @param request  接收用户传过来的信息
	 * @return  返回在分页
	 */
	protected PageInfoUtil proccedPageInfoUtil(HttpServletRequest request)
	{
		/*分页*/
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		try
		{
			String currentPage =  request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		}catch(NumberFormatException e)
		{
			
		}
		return pageInfoUtil ;
	}
}
